home *** CD-ROM | disk | FTP | other *** search
- /*##########################################################################
- Copyright 2009 Tim Reid
-
- This file is part of the Stack Overflow Reputation Display extension
- for Mozilla Firefox.
-
- Stack Overflow Reputation Display is free software: you can
- redistribute it and/or modify it under the terms of the GNU General
- Public License as published by the Free Software Foundation, either
- version 3 of the License, or (at your option) any later version.
-
- Stack Overflow Reputation Display is distributed in the hope that it
- will be useful, but WITHOUT ANY WARRANTY; without even the implied
- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- See the GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public
- License along with Stack Overflow Reputation Display. If not,
- see <http://www.gnu.org/licenses/>.
- ##########################################################################*/
-
- var SODisplay = function (init) {
- this.id = "autoid" + SODisplay.counter++;
- this.currentscore = {};
- this.document = document;
- this.requests = [];
-
- this.elements = this.buildbasic(this.document);
-
- if (init)
- for (var k in init)
- this[k] = init[k];
-
- var me = this;
- for (var i=0; i<this.elements.clicktargets.length; i++)
- this.elements.clicktargets[i].addEventListener("click", function (e) { me.doclick(e); }, true);
-
- return this;
- };
-
- SODisplay.counter = 0;
-
- SODisplay.prototype = {
- opener: null,
- textcolor: null,
-
- buildbasic: function (doc) {
- var elements = {
- reputation: {
- text: [],
- },
- name: {
- text: [],
- },
- siteicons: [],
- usericons: [],
- clicktargets: [],
- badgecontainers: {},
- badgetext: {},
- menuitems: {},
- textitems: {
- statusbar: [],
- popup: [],
- },
- };
-
- //######################################################################
- // Create popups for the display
- //######################################################################
- elements.panel = doc.createElement("statusbarpanel");
- elements.panel.setAttribute("class", "sorepdisplay");
- this.tooltipvisible = false;
-
- var h = doc.createElement("hbox");
- h.setAttribute("align", "center");
- h.setAttribute("class", "sorepdisplay sorepdisplay-statusbarpanelcontent");
- elements.panel.appendChild(h);
-
- var i = doc.createElement("image");
- i.setAttribute("class", "sorepdisplay sorepdisplay-soicon");
- i.setAttribute("height", 16);
- i.setAttribute("width", 16);
- h.appendChild(i);
- elements.siteicons.push(i);
- elements.clicktargets.push(i);
-
- var d = doc.createElement("description");
- d.setAttribute("tooltiptext", "reputation score");
- d.setAttribute("class", "sorepdisplay sorepdisplay-score reputationtextcolor");
- d.appendChild(doc.createTextNode("-"));
- h.appendChild(d);
- elements.reputation.text.push(d);
- elements.textitems.popup.push(d);
- elements.h = h;
-
- //######################################################################
- // Create popups for the display
- //######################################################################
- elements.popupset = doc.createElement("popupset");
- elements.popupset.setAttribute("class", "sorepdisplay");
-
- //######################################################################
- // Create tooltip popup
- //######################################################################
- var t = doc.createElement("tooltip");
- t.setAttribute("id", "sorepdisplay-flair-" + this.id);
- t.setAttribute("class", "sorepdisplay");
- t.setAttribute("position", "before_end");
- elements.popupset.appendChild(t);
-
- var h3 = doc.createElement("hbox");
- h3.setAttribute("width", "200");
- t.appendChild(h3);
-
- var v = doc.createElement("vbox");
- h3.appendChild(v);
-
- var i2 = doc.createElement("image");
- i2.setAttribute("width", "50");
- i2.setAttribute("height", "50");
- v.appendChild(i2);
- elements.usericons.push(i2);
-
- var s = doc.createElement("spacer");
- s.setAttribute("flex", "1");
- v.appendChild(s);
-
- var s2 = doc.createElement("spacer");
- s2.setAttribute("flex", "1");
- h3.appendChild(s2);
-
- var v2 = doc.createElement("vbox");
- v2.setAttribute("align", "end");
- h3.appendChild(v2);
-
- var h4 = doc.createElement("hbox");
- v2.appendChild(h4);
-
- var i3 = doc.createElement("image");
- i3.setAttribute("height", 16);
- i3.setAttribute("width", 16);
- h4.appendChild(i3);
- elements.siteicons.push(i3);
-
- var d3 = doc.createElement("description");
- d3.setAttribute("class", "sorepdisplay sorepdisplay-name");
- d3.appendChild(doc.createTextNode("-"));
- h4.appendChild(d3);
- elements.name.text.push(d3);
- elements.textitems.popup.push(d3);
-
- var d4 = doc.createElement("description");
- d4.setAttribute("class", "sorepdisplay sorepdisplay-score defaultreputationtextcolor");
- d4.appendChild(doc.createTextNode("-"));
- v2.appendChild(d4);
- elements.reputation.text.push(d4);
- elements.textitems.popup.push(d4);
-
- var h5 = doc.createElement("hbox");
- v2.appendChild(h5);
- elements.h5 = h5;
-
- //######################################################################
- // Create context popup
- //######################################################################
- var mp = doc.createElement("menupopup");
- mp.setAttribute("id", "sorepdisplay-popup-" + this.id);
- elements.panel.setAttribute("context", "sorepdisplay-popup-" + this.id);
- elements.popupset.appendChild(mp);
-
- elements.menuitems.options = doc.createElement("menuitem");
- elements.menuitems.options.setAttribute("accesskey", "C");
- elements.menuitems.options.setAttribute("label", "Configure...");
- mp.appendChild(elements.menuitems.options);
-
- return elements;
- },
-
- removebadges: function (badges, badgecontainers, badgetext) {
- for (var i=0; i<badges.length; i++) {
- var list = badgecontainers[badges[i]];
- if (list)
- for (var j=0; j<list.length; j++)
- list[j].parentNode.removeChild(list[j]);
- delete badgecontainers[badges[i]];
- }
- },
-
- addbadges: function (badges, element, badgecontainers, badgetext, badgetextclass) {
- var doc = element.ownerDocument;
- for (var k=0; k<badges.length; k++) {
- if (!(badges[k] in badgecontainers))
- badgecontainers[badges[k]] = [];
- if (!(badges[k] in badgetext))
- badgetext[badges[k]] = [];
-
- var hbox = doc.createElement("hbox");
- hbox.setAttribute("tooltiptext", badges[k] + " badges");
- hbox.setAttribute("align", "center");
- hbox.setAttribute("hidden", "true");
- element.appendChild(hbox);
- badgecontainers[badges[k]].push(hbox);
-
- var bullet = doc.createElement("description");
- bullet.setAttribute("class", "sorepdisplay sorepdisplay-badge" + (k+1));
- bullet.appendChild(doc.createTextNode("\u25cf"));
- hbox.appendChild(bullet);
-
- var text = doc.createElement("description");
- text.setAttribute("class", "sorepdisplay sorepdisplay-badgecount " + badgetextclass);
- text.appendChild(doc.createTextNode("-"));
- hbox.appendChild(text);
- badgetext[badges[k]].push(text);
- }
- },
-
- get account () {
- return this._account;
- },
-
- set account (account) {
- this._account = account;
- var badges = this.account.site.badges;
- this.removebadges(badges, this.elements.badgecontainers, this.elements.badgetext);
- this.addbadges(badges, this.elements.h, this.elements.badgecontainers, this.elements.badgetext, "badgetextcolor");
- this.addbadges(badges, this.elements.h5, this.elements.badgecontainers, this.elements.badgetext, "defaultbadgetextcolor");
- for (var i=0; i<this.elements.siteicons.length; i++)
- this.elements.siteicons[i].setAttribute("src", this.account.site.smallicon);
- },
-
- showtooltip: function (b) {
- if (b) {
- this.elements.panel.setAttribute("tooltip", "sorepdisplay-flair-" + this.id);
- this.tooltipvisible = true;
- } else {
- this.elements.panel.removeAttribute("tooltip");
- this.tooltipvisible = false;
- }
- },
-
- preload: function (url) {
- var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
- .createInstance(Components.interfaces.nsIXMLHttpRequest);
- req.open("GET", url, true);
- req.onload = function (e) {};
- req.send(null);
- if (this.requests)
- this.requests.push(req);
- },
-
- get datauri () {
- return this.account.site.uri("data", this.account.user);
- },
-
- formatscore: function (score) {
- var text = score.toString().replace(/,/g, "");
- var newtext = text.replace(/(.*[0-9])([0-9][0-9][0-9])$/, "$1,$2");
- do {
- text = newtext;
- newtext = text.replace(/(.*[0-9])([0-9][0-9][0-9]),$/, "$1,$2");
- } while (text != newtext);
-
- return newtext;
- },
-
- summary: function (count, colour) {
- var text = count + " " + colour + " badge";
- if (count != 1)
- text += "s";
- return text;
- },
-
- updatedetails: function () {
- var change;
- var formattedreputation;
-
- var info = this.account.info;
-
- if (!info)
- return;
-
- if (info.reputation) {
- formattedreputation = this.formatscore(info.reputation);
-
- if ("reputation" in this.currentscore) {
- if (info.reputation > this.currentscore.reputation) {
- change = change || "up";
- } else if (info.reputation < this.currentscore.reputation) {
- change = change || "down";
- }
- }
- } else {
- formattedreputation = "-";
- }
-
- if (info.avatar) {
- var images = this.elements.usericons;
- for (var j=0; j<images.length; j++)
- if (images[j].getAttribute("src") != info.avatar) {
- images[j].setAttribute("src", info.avatar);
- this.preload(info.avatar);
- }
- }
-
- if (info.name) {
- var names = this.elements.name.text;
- for (var j=0; j<names.length; j++)
- if (names[j].getAttribute("value") != info.name)
- names[j].setAttribute("value", info.name);
- }
-
- var reputations = this.elements.reputation.text;
- for (var i=0; i<reputations.length; i++)
- if (reputations[i].getAttribute("value") != formattedreputation)
- reputations[i].setAttribute("value", formattedreputation);
-
- var badges = this.account.site.badges;
- for (var i=0; i<badges.length; i++) {
- var badge = badges[i];
- if (info[badge]) {
- var text = this.formatscore(info[badge]);
- var texts = this.elements.badgetext[badge];
- for (var j=0; j<texts.length; j++)
- if (texts[j].getAttribute("value") != text)
- texts[j].setAttribute("value", text);
-
- var tooltip = this.summary(text, badge);
- var containers = this.elements.badgecontainers[badge];
- for (var j=0; j<containers.length; j++) {
- if (containers[j].getAttribute("tooltiptext") != tooltip)
- containers[j].setAttribute("tooltiptext", tooltip);
- containers[j].setAttribute("hidden", false);
- }
-
- if (this.currentscore[badge]) {
- if (info[badge] > this.currentscore[badge]) {
- change = change || "up";
- } else if (info[badge] < this.currentscore[badge]) {
- change = change || "down";
- }
- }
- } else {
- var containers = this.elements.badgecontainers[badge];
- if (containers)
- for (var j=0; j<containers.length; j++)
- containers[j].setAttribute("hidden", true);
- }
- }
-
- if (!this.tooltipvisible)
- this.showtooltip(true);
- },
-
- doclick: function (e) {
- if (e.button == 0)
- if (this.opener) {
- var uri = this.account.site.uri(this.account.clickaction, this.account.user) ||
- this.account.site.uri("main");
- if (uri)
- this.opener(uri);
- }
- },
-
- registeropener: function (f) {
- this.opener = f;
- },
- };
-